package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.vo.ExportProductVo;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.domain.vo.ExportVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController{
    @Reference
    private ExportService exportService;
    @Reference
    private ContractService contractService;
    @Reference
    private ExportProductService exportProductService;
    @RequestMapping(value = "/contractList",name = "进入到合同管理界面")
    public String contractList(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int pages){
        ContractExample contractExample = new ContractExample();
        contractExample.setOrderByClause("create_time desc");
        contractExample.createCriteria().andCompanyIdEqualTo(getCompanyId()).andStateEqualTo(1);
        PageInfo pageInfo = contractService.findAll(contractExample, page, pages);
        request.setAttribute("page",pageInfo);
        return "cargo/export/export-contractList";
    }
    @RequestMapping(value = "toExport",name = "跳转到报运界面")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }
    @RequestMapping(value = "/list",name = "出口报运界面")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int pages){
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andCompanyIdEqualTo(getCompanyId());
        exportExample.setOrderByClause("create_time desc");
        PageInfo pageInfo = exportService.findAll(exportExample, page, pages);
        request.setAttribute("page",pageInfo);
        return "cargo/export/export-list";
    }
    @RequestMapping(value = "/edit",name = "新增和编辑界面")
    public String edit(Export export){
        if(StringUtils.isBlank(export.getId())){
            export.setId(UUID.randomUUID().toString());
            export.setCreateDept(getUser().getDeptId());
            export.setCreateBy(getUser().getId());
            export.setCreateTime(new Date());
            export.setCompanyId(getCompanyId());
            export.setCompanyName(getCompanyName());
            export.setState(0);
            exportService.save(export);
        }else {
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到编辑界面")
    public String toUpdate(String id){
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        List<ExportProduct> exportProduct=exportProductService.findByExportId(id);
        request.setAttribute("eps",exportProduct);
        return "cargo/export/export-update";
    }
    @RequestMapping(value = "/exportE",name = "电子报运")
    public String exportE(String id){
        ExportVo exportVo = new ExportVo();
        exportVo.setExportId(id);
        Export export = exportService.findById(id);
        BeanUtils.copyProperties(export,exportVo);
        export.setState(2);
        exportService.update(export);
        List<ExportProductVo> list=new ArrayList<>();
        List<ExportProduct> exportProductList = exportProductService.findByExportId(id);
        for (ExportProduct exportProduct : exportProductList) {
            ExportProductVo exportProductVo = new ExportProductVo();
            exportProductVo.setExportProductId(exportProduct.getId());
            BeanUtils.copyProperties(exportProductVo,exportProduct);
            exportProductVo.setEid(exportVo.getId());
            list.add(exportProductVo);
        }
        exportVo.setProducts(list);
        WebClient.create("http://localhost:9090/jk/ws/export/ep").post(exportVo);
        ExportResult exportResult=WebClient.create("http://localhost:9090/jk/ws/export/ep/"+id).get(ExportResult.class);
        exportService.updateE(exportResult);
        return "redirect:/cargo/export/list.do";
    }
    @RequestMapping(value = "/cancel",name = "取消报运")
    public String cancel(String id){
        Export export = exportService.findById(id);
        export.setState(0);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }
    @RequestMapping(value = "/toView",name = "查看")
    public String toView(String id){
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        return "cargo/export/export-view";
    }
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        exportService.delete(id);
        return "redirect:/cargo/export/list.do";
    }
}
